package com.lucweb.dao.impl;


import com.lucweb.dao.WhyjtaskDao;
import com.lucweb.entity.*;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Repository("whyjtaskDao")
public class WhyjtaskDaoImpl implements WhyjtaskDao {

    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;

    private JctaskIndex jctaskIndex;

    private TaskBean taskBean;



    @Override
    public List getAllwhyjTask() {
        //处理活的的检测任务列表、
        List<JctaskIndex> taskList = sessionFactory.getCurrentSession().createSQLQuery(
                "SELECT sys_jctask.task_id,sys_jctask.task_name,(SELECT count(question_id) FROM sys_question " +
                "WHERE question_id_task_id\n" +
                " = sys_jctask.task_id) AS question_count ,(SELECT count(question_id) FROM sys_question WHERE\n" +
                "question_id_task_id = sys_jctask.task_id AND question_solve = \"1\" )\n" +
                "AS question_solve,(SELECT count(question_id) FROM sys_question WHERE\n" +
                "question_id_task_id = sys_jctask.task_id  AND question_difficult = \"1\" ) AS  question_diff ,\n" +
                "task_time,task_people FROM sys_jctask")
                .addScalar("task_id",StandardBasicTypes.STRING)
                .addScalar("question_count", StandardBasicTypes.STRING)
                .addScalar("task_name",StandardBasicTypes.STRING)
                .addScalar("task_time",StandardBasicTypes.STRING)
                .addScalar("task_people",StandardBasicTypes.STRING)
                .addScalar("question_solve",StandardBasicTypes.STRING)
                .addScalar("question_diff",StandardBasicTypes.STRING)
                .setResultTransformer(Transformers.aliasToBean(JctaskIndex.class)).list();

        return taskList;
    }

    @Override
    public List<TaskWhyj> WhyjTask(String para) {
//        Jctask jctask = (Jctask) sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM sys_jctask WHERE task_id = '1'")
//                .setResultTransformer(Transformers.aliasToBean(Jctask.class));


        List<TaskWhyj> whyjList = (List<TaskWhyj>) sessionFactory.getCurrentSession().createQuery("FROM TaskWhyj where "+para).list();



        return whyjList;
    }


    @Override
    public List<TaskWhyjtz> showTzList(String company) {
//        Jctask jctask = (Jctask) sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM sys_jctask WHERE task_id = '1'")
//                .setResultTransformer(Transformers.aliasToBean(Jctask.class));


        List<TaskWhyjtz> whyjTzList = (List<TaskWhyjtz>) sessionFactory.getCurrentSession().createQuery("FROM TaskWhyjtz where companyName ="+company+
                "and ischecked = '1'").list();



        return whyjTzList;
    }

    @Override
    public List<String> showDtList(String company) {
//        Jctask jctask = (Jctask) sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM sys_jctask WHERE task_id = '1'")
//                .setResultTransformer(Transformers.aliasToBean(Jctask.class));


        List<String> dtList = (List<String>) sessionFactory.getCurrentSession().createSQLQuery("select DISTINCT tzdtName FROM task_whyjtz WHERE "+company)
                .list();



        return dtList;
    }


    @Override
    public List<TaskWhyjtz> showTzFinish(String company) {
//        Jctask jctask = (Jctask) sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM sys_jctask WHERE task_id = '1'")
//                .setResultTransformer(Transformers.aliasToBean(Jctask.class));


        List<TaskWhyjtz> whyjTzList = (List<TaskWhyjtz>) sessionFactory.getCurrentSession().createQuery("FROM TaskWhyjtz where companyName ="+company+
                " and ischecked = '1' and isFinish = '1'").list();



        return whyjTzList;
    }

    @Override
    public String WhyjTaskNumber() {
        String number = (String) sessionFactory.getCurrentSession().createSQLQuery("SELECT max(taskNumber) as max FROM task_whyj")
                .addScalar("max",StandardBasicTypes.STRING).uniqueResult();


        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        if (number == null) {
            number = formatter.format(date) + "3001";
        } else {
            DecimalFormat df = new DecimalFormat("0000");
            number = formatter.format(date)
                    + df.format(1 + Integer.parseInt(number.substring(8, 12)));

        }


        return number;
    }

    @Override
    public void saveOrUpdate(TaskWhyj taskWhyj) {
         sessionFactory.getCurrentSession().saveOrUpdate(taskWhyj);
    }

    @Override
    public void saveTz(TaskWhyjtz taskWhyjtz) {
        sessionFactory.getCurrentSession().saveOrUpdate(taskWhyjtz);
    }



    @Override
    public List<TaskTz> showTz() {
        return sessionFactory.getCurrentSession().createQuery("FROM TaskTz ").list();
    }

    @Override
    public TaskWhyj getWhyjTaskById(int taskId) {
        return (TaskWhyj)sessionFactory.getCurrentSession().get(TaskWhyj.class, taskId);

    }

    @Override
    public void deleteWhyjtask(int taskId) {
        sessionFactory.getCurrentSession().delete(
                sessionFactory.getCurrentSession().get(TaskWhyj.class, taskId)
        );
    }

    @Override
    public void deleteTzDt(String para) {
        sessionFactory.getCurrentSession().createSQLQuery("Delete from task_Whyjtz where "+para).executeUpdate();
    }

    @Override
    public int getmaxId() {
        Integer number = (Integer) sessionFactory.getCurrentSession().createSQLQuery("select auto_increment from " +
                "information_schema.tables where table_schema='firesystem' and table_name='task_whyjtz';")
                .addScalar("auto_increment", StandardBasicTypes.INTEGER).uniqueResult();
        return number;

    }

}
